package com.wyl.mine.userinfo

import android.content.Intent
import android.widget.Toast
import androidx.databinding.ObservableField
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.ACacheHelper
import com.wyl.base.COMMON_GETCODE
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.toast
import com.wyl.libbase.utils.CountDownUtils
import com.wyl.libbase.utils.Validator
import com.wyl.mine.R
import com.wyl.mine.databinding.EditPhoneActivityBinding
import org.json.JSONObject

class EditPhoneActivity : BindingActivity<EditPhoneActivityBinding>() {

    val phone: ObservableField<String> = ObservableField()
    val code: ObservableField<String> = ObservableField()
    val newPhone: ObservableField<String> = ObservableField()

    override fun getLayoutId(): Int = R.layout.edit_phone_activity

    override fun initView() {
        binding.vm = this

        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }
    }

    override fun loadData() {

    }


    fun commit() {
        if (phone.get().isNullOrEmpty()) {
            toast(getString(R.string.ui_phone_old_input))
            return
        }
        if (!Validator.isMobile(phone.get())) {
            toast("原手机号格式不正确")
            return
        }
        if (code.get().isNullOrEmpty()) {
            toast(getString(R.string.code_input))
            return
        }
        if (ACacheHelper.getMsgCode() != code.get()) {
            toast("验证码不正确", Toast.LENGTH_LONG)
            return
        }
        if (newPhone.get().isNullOrEmpty()) {
            toast(getString(R.string.ui_phone_old_input))
            return
        }
        if (!Validator.isMobile(newPhone.get())) {
            toast("新手机号格式不正确")
            return
        }
        setResult(RESULT_OK, Intent().putExtra("content", newPhone.get()))
        onBackPressed()
    }

    fun getMessageCode() {
        if (phone.get().isNullOrEmpty()) {
            toast(getString(R.string.ui_phone_old_input))
            return
        }
        if (!Validator.isMobile(phone.get())) {
            toast("手机号格式不正确")
            return
        }

        OkGo.post<String>(COMMON_GETCODE)
            .params("phone", phone.get())
            .execute(object : StringCallback() {
                override fun onSuccess(response: Response<String>) {
                    val obj = JSONObject(response.body())
                    if (obj.optInt("msg") == 0) {
                        //成功
                        toast("获取验证码成功")
                        ACacheHelper.putCode(obj.optString("code"))
                        CountDownUtils.changeBtnUi(binding.btGetCode, 60, getString(R.string.ui_code_obtain))
                    } else
                        toast("获取验证码失败", Toast.LENGTH_LONG)
                }

                override fun onError(response: Response<String>) {
                    toast("请求失败", Toast.LENGTH_LONG)
                }

                override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) = showLoadingDialog()

                override fun onFinish() = dismissLoadingDialog()

            })
    }
}
